package fetcher

import (
	"fmt"
	"io/ioutil"
	"log"
	"math/rand"
	"net/http"
	"net/url"
	"parser/engine"
)

type ReqFetch struct {
	ProxyFile   string
	prixyPool  []string
	statusProxyChan chan ProxySource
}
type ProxySource struct {
	Proxy string
	Source bool
}

func GetFetcher (proxyFile string) *ReqFetch{
	r:=  &ReqFetch{ProxyFile:proxyFile}
	r.ProxyDo()
	return  r
}

func (f ReqFetch) Worker(r *engine.Request) (engine.ParserResult, error) {

	urli := url.URL{}
	Proxy := f.GetPorxy()
	c := &http.Client{}
	result := engine.ParserResult{}
	if Proxy != "" {
		//fmt.Println("set proxy" + Proxy)
		//time.After(time.Second*5)
		urlproxy, _ := urli.Parse("http://" + Proxy)
		c.Transport = &http.Transport{
			Proxy: http.ProxyURL(urlproxy),
		}
	}
	//else{
	//	log.Printf("%+v",f)
	//	log.Fatal("dididididididie")
	//}

	req, err := http.NewRequest("GET", r.Url, nil)

	if err != nil{
		log.Println("Fail New Request error  resion:http new request !",err)
		return result, err
	}


	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
	resp, err := c.Do(req)
	if err != nil {
		f.statusProxyChan<-ProxySource{Proxy:Proxy,Source:false}
		log.Println("Fail  - resion:http get !\n",err)
		return result, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		log.Println("Fail  - resion:http status !\n",resp.StatusCode)
		return result, fmt.Errorf("wrong status code : %d", resp.StatusCode)
	}
	content,err :=ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println("Fail  - resion:http read err !\n",err)
		return result,err
	}
	f.statusProxyChan<-ProxySource{Proxy:Proxy,Source:true}
	return r.ParserFunc(content),err
}

func (f *ReqFetch) GetPorxy() string {
	return f.prixyPool[rand.Intn(len(f.prixyPool))]  //go routing  存在并发删除问题
}



func (f *ReqFetch) ProxyDo()   {
	if f.ProxyFile == ""{
		f.ProxyFile = "proxy.log"
	}
	b, err := ioutil.ReadFile(f.ProxyFile )
	if err != nil {
		fmt.Println(f.ProxyFile)
		panic(err)
	}
	f.prixyPool = make([]string ,0,1000)

	s:=""
	for _, value := range b {
		if value == ',' {
			if s =="" {
				continue
			}
			f.prixyPool = append(f.prixyPool,s)
			s = ""
			continue
		}
		s += string(value)
	}
	if s != "" {
		f.prixyPool = append(f.prixyPool,s)
	}
	if len(f.prixyPool) <=0 {
		log.Fatal("proxy pool is poor!!")
	}
	f.statusProxyChan = make(chan ProxySource)
	go func() {
		for  {
			proxy:= <-f.statusProxyChan
			fmt.Printf("%#v\n",proxy)
		}

	}()
}